package com.code;

import android.content.Context;
import android.opengl.GLES30;
import android.os.Build;
import android.support.annotation.RequiresApi;

import com.graphics.GLGraphics;
import com.graphics.GLRender;
import com.graphics.element.AbsSceneNode;
import com.system.Camera;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/**
 * @Author: clm
 * @Date: 2018-10-27
 */

public abstract class AbsGLRender extends GLRender {
    public Camera mCamera;
    protected AbsSceneNode mRootNode;
    protected Context mContext;
    public AbsGLRender(Context context){
        mContext = context;
        mCamera = getCamera();
        GLGraphics.setCamera(mCamera);
    }
    //创建gl_context时
    protected abstract void onCreate(GL10 gl10, EGLConfig eglConfig);
    //创建摄像机
    protected abstract Camera getCamera();
    //创建根节点
    protected abstract void onCreateRootNode();
    //初始化数据
    protected abstract void onInitChange(GL10 gl10, int w, int h);
    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
        super.onSurfaceCreated(gl10, eglConfig);
        onCreate(gl10, eglConfig);
        mRootNode = new AbsSceneNode.RootNode("rootNode");
        onCreateRootNode();
        mIGLLooper.init();
    }
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public void onSurfaceChanged(GL10 gl10, int w, int h) {
        super.onSurfaceChanged(gl10, w, h);
        mCamera.setP_matrix(mP_matrix);
        onInitChange(gl10, w, h);
    }
    @Override
    public void onDrawFrame(GL10 gl10) {
        super.onDrawFrame(gl10);
        //gles2.x不支持vao,只能用vbo
        //gles3.x要求一定要使用vao
        GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT | GLES30.GL_DEPTH_BUFFER_BIT);

        //update and render
        mRootNode.updateStart((float) exTime);
        mRootNode.renderStart((float) exTime);

        //mSkyCube.render(0);
        GLES30.glFinish();
    }
}
